package com.ruoyi.blog.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.blog.domain.TbBlog;
import com.ruoyi.blog.domain.TbBlogCategory;
import com.ruoyi.blog.mapper.TbBlogCategoryMapper;
import com.ruoyi.blog.mapper.TbBlogMapper;
import com.ruoyi.blog.service.ITbBlogService;
import com.ruoyi.common.enums.BlogStatus;
import com.ruoyi.common.enums.DeleteStatus;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.IdWorker;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 文章Service业务层处理
 *
 * @author ruoyi
 * @date 2024-03-08
 */
@Service
public class TbBlogServiceImpl extends ServiceImpl<TbBlogMapper, TbBlog> implements ITbBlogService {
    @Autowired
    private TbBlogMapper tbBlogMapper;
    @Autowired
    private TbBlogCategoryMapper tbBlogCategoryMapper;
    @Autowired
    private IdWorker idWorker;

    /**
     * 查询文章
     *
     * @param blogId 文章主键
     * @return 文章
     */
    @Override
    public TbBlog selectTbBlogByBlogId(String blogId) {
        return tbBlogMapper.selectTbBlogByBlogId(blogId);
    }

    /**
     * 查询文章列表
     *
     * @param tbBlog 文章
     * @return 文章
     */
    @Override
    public List<TbBlog> selectTbBlogList(TbBlog tbBlog) {
        return tbBlogMapper.selectTbBlogList(tbBlog);
    }

    /**
     * 新增文章
     *
     * @param tbBlog 文章
     * @return 结果
     */
    @Override
    public int insertTbBlog(TbBlog tbBlog) {
        tbBlog.setBlogId(idWorker.nextId() + "");
        tbBlog.setCreateTime(DateUtils.getNowDate());
        tbBlog.setUpdateTime(DateUtils.getNowDate());
        tbBlog.setIsDeleted(DeleteStatus.UN_DELETE.getCode());
        tbBlog.setBlogStatus(BlogStatus.WAIT_VERIFY.getCode());
        tbBlog.setLiked(0);
        tbBlog.setUnliked(0);
        tbBlog.setIsShowLiked(true);
        tbBlog.setIsShowUnliked(true);
        selectCategoryName(tbBlog);
        return tbBlogMapper.insertTbBlog(tbBlog);
    }

    /**
     * 修改文章
     *
     * @param tbBlog 文章
     * @return 结果
     */
    @Override
    public int updateTbBlog(TbBlog tbBlog) {
        tbBlog.setUpdateTime(DateUtils.getNowDate());

        selectCategoryName(tbBlog);
        return tbBlogMapper.updateTbBlog(tbBlog);
    }

    private void selectCategoryName(TbBlog tbBlog) {
        String blogCategoryId = tbBlog.getBlogCategoryId();
        if (StringUtils.isNotBlank(blogCategoryId)) {
            TbBlogCategory tbBlogCategory = tbBlogCategoryMapper.selectTbBlogCategoryByCategoryId(blogCategoryId);
            String categoryName = tbBlogCategory.getCategoryName();
            tbBlog.setBlogCategoryName(categoryName);
        }
    }

    /**
     * 批量删除文章
     *
     * @param blogIds 需要删除的文章主键
     * @return 结果
     */
    @Override
    public int deleteTbBlogByBlogIds(String[] blogIds) {
        return tbBlogMapper.deleteTbBlogByBlogIds(blogIds);
    }

    /**
     * 删除文章信息
     *
     * @param blogId 文章主键
     * @return 结果
     */
    @Override
    public int deleteTbBlogByBlogId(String blogId) {
        return tbBlogMapper.deleteTbBlogByBlogId(blogId);
    }
}
